package com.sikao.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.MallBean;
import com.sikao.app.utils.DimenTool;
import com.sikao.app.utils.SizeConvertUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/8/22.
 */
public class SupplyAdapter extends BaseAdapter {


	private List<MallBean.ResBean.ListBean> objects;
	private Context context;
	private LayoutInflater layoutInflater;

	public SupplyAdapter(Context context, List<MallBean.ResBean.ListBean> objects) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.objects = objects;


	}

	@Override
	public int getCount() {
		return objects.size();
	}

	@Override
	public MallBean.ResBean.ListBean getItem(int position) {
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder;
		if (convertView == null){
			view = View.inflate(context, R.layout.business_list_item,null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		}else{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}
		if (position == 0){
			holder.viewLine.setVisibility(View.GONE);
		}else {
			holder.viewLine.setVisibility(View.VISIBLE);
		}
		initializeViews(objects.get(position),holder);
		return view;
	}

	private void initializeViews(MallBean.ResBean.ListBean listBean, ViewHolder holder) {
		//TODO implement

		holder.nameTv.setText(listBean.getTitle());

		//1仓单 2保证金 3信誉
		if ("1".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_orange);
			holder.releaseTypeTv.setText("仓单挂单");
		}
		if ("2".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_blue_light);
			holder.releaseTypeTv.setText("保证金挂单");
		}
		if ("3".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_yellow);
			holder.releaseTypeTv.setText("信誉挂单");
		}

		holder.timeTv.setText(listBean.getAdd_time_format());
		holder.priceTv.setText(listBean.getPrice());
		holder.priceUnitTv.setText("元/"+listBean.getUnit());
		holder.quantityTv.setText("数量"+listBean.getNumber()+listBean.getUnit());
		if (listBean.getIs_ported().equals("是")){
			holder.canSplitTv.setVisibility(View.VISIBLE);
		}else{
			holder.canSplitTv.setVisibility(View.GONE);
		}
		holder.deliveryPlaceTv.setText(listBean.getDelivery_area());
		if (listBean.getDelivery_type().equals("1")) {
			holder.deliveryModeTv.setText("买家自提");
		} else {
			holder.deliveryModeTv.setText("送货上门");
		}
	}

	public class ViewHolder {
		private TextView nameTv;
		private TextView releaseTypeTv;
		private TextView canSplitTv;
		private TextView timeTv;
		private TextView priceTv;
		private TextView priceUnitTv;
		private TextView quantityTv;
		private TextView deliveryPlaceTv;
		private TextView deliveryModeTv;
		public final View root;
		public final View viewLine;

		public ViewHolder(View root) {
			nameTv = (TextView) root.findViewById(R.id.type_tv);
			releaseTypeTv = (TextView) root.findViewById(R.id.release_type_tv);
			canSplitTv = (TextView) root.findViewById(R.id.can_split_tv);
			timeTv = (TextView) root.findViewById(R.id.time_tv);
			priceTv = (TextView) root.findViewById(R.id.price_tv);
			priceUnitTv = (TextView) root.findViewById(R.id.price_unit_tv);
			quantityTv = (TextView) root.findViewById(R.id.quantity_tv);
			deliveryPlaceTv = (TextView) root.findViewById(R.id.delivery_place_tv);
			deliveryModeTv = (TextView) root.findViewById(R.id.delivery_mode_tv);
			viewLine = root.findViewById(R.id.view_line);
			this.root = root;
		}
	}
}
